package com.ocom.payhandle.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.master.EntityCompanykeyInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoCompanyKeyInfo;
import com.ocom.payhandle.service.CompanyKeyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class CompanyKeyInfoServiceImpl extends ServiceImpl<IDaoCompanyKeyInfo, EntityCompanykeyInfo> implements CompanyKeyInfoService {

    //注入MngUserInfo 持久层
    @Autowired
    private IDaoCompanyKeyInfo daoCompanyKeyInfo;

    /**
     * 从总库单位密钥信息表company_key_info查找com_id= companyID的valid_date最大的3条记录
     *
     * @return*/
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public Map<String, Object> selectByPublicKey(Long companyId) {
        QueryWrapper<EntityCompanykeyInfo> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.orderByDesc("create_time").last("limit 3");
        List<EntityCompanykeyInfo> entityCompanykeyInfos = daoCompanyKeyInfo.selectList(query);
        Map<String,Object> secret = new HashMap<>();
        if(entityCompanykeyInfos.size() == 1){
            for (EntityCompanykeyInfo entityCompanykeyInfo: entityCompanykeyInfos) {
                //标识本次通讯
                secret.put("public_key", entityCompanykeyInfo.getPublicKey());
                secret.put("prev_public", entityCompanykeyInfo.getPublicKey());
                secret.put("next_public", entityCompanykeyInfo.getPublicKey());

                secret.put("private_pri", entityCompanykeyInfo.getPrivateKey());
                secret.put("prev_pri", entityCompanykeyInfo.getPrivateKey());
                secret.put("next_pri", entityCompanykeyInfo.getPrivateKey());
            }
        }
        if(entityCompanykeyInfos.size() == 2){
            for(int i=0;i<entityCompanykeyInfos.size();i++){
                if(i == 0){
                    secret.put("next_public", entityCompanykeyInfos.get(i).getPublicKey());
                    secret.put("next_pri", entityCompanykeyInfos.get(i).getPrivateKey());
                }else{
                    secret.put("public_key", entityCompanykeyInfos.get(i).getPublicKey());
                    secret.put("private_pri", entityCompanykeyInfos.get(i).getPublicKey());

                    secret.put("prev_public", entityCompanykeyInfos.get(i).getPrivateKey());
                    secret.put("prev_pri", entityCompanykeyInfos.get(i).getPrivateKey());
                }
            }
        }
        if(entityCompanykeyInfos.size() == 3){
            for(int i=0;i<entityCompanykeyInfos.size();i++){
                if(i == 0){
                    secret.put("next_public", entityCompanykeyInfos.get(i).getPublicKey());
                    secret.put("next_pri", entityCompanykeyInfos.get(i).getPrivateKey());
                }else if(i == 1){
                    secret.put("public_key", entityCompanykeyInfos.get(i).getPublicKey());
                    secret.put("private_pri", entityCompanykeyInfos.get(i).getPrivateKey());
                }else {
                    secret.put("prev_public", entityCompanykeyInfos.get(i).getPublicKey());
                    secret.put("prev_pri", entityCompanykeyInfos.get(i).getPrivateKey());
                }
            }
        }

        return secret;
    }
}
